[[...path]].page.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import React, { useEffect } from 'react';
  2. import { type IPagePopulatedToShowRevision, getIdForRef } from '@growi/core';
  3. import type {
  4. GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import dynamic from 'next/dynamic';
  8. import Head from 'next/head';
  9. import superjson from 'superjson';
  10. import { ShareLinkLayout } from '~/components-universal/Layout/ShareLinkLayout';
  11. import { DrawioViewerScript } from '~/components-universal/Script/DrawioViewerScript';
  12. import { ShareLinkPageView } from '~/components-universal/ShareLinkPageView';
  13. import type { SupportedActionType } from '~/interfaces/activity';
  14. import { SupportedAction } from '~/interfaces/activity';
  15. import type { CrowiRequest } from '~/interfaces/crowi-request';
  16. import type { RendererConfig } from '~/interfaces/services/renderer';
  17. import type { IShareLinkHasId } from '~/interfaces/share-link';
  18. import type { PageDocument } from '~/server/models/page';
  19. import ShareLink from '~/server/models/share-link';
  20. import {
  21. useCurrentUser, useRendererConfig, useIsSearchPage, useCurrentPathname,
  22. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useIsContainerFluid, useIsEnabledMarp,
  23. } from '~/stores-universal/context';
  24. import { useCurrentPageId, useIsNotFound, useSWRMUTxCurrentPage } from '~/stores/page';
  25. import loggerFactory from '~/utils/logger';
  26. import type { NextPageWithLayout } from '../_app.page';
  27. import type { CommonProps } from '../utils/commons';
  28. import {
  29. getServerSideCommonProps, generateCustomTitleForPage, getNextI18NextConfig, skipSSR, addActivity,
  30. } from '../utils/commons';
  31. const GrowiContextualSubNavigationSubstance = dynamic(() => import('~/components/Navbar/GrowiContextualSubNavigation'), { ssr: false });
  32. const logger = loggerFactory('growi:next-page:share');
  33. type Props = CommonProps & {
  34. shareLinkRelatedPage?: IShareLinkRelatedPage,
  35. shareLink?: IShareLinkHasId,
  36. isNotFound: boolean,
  37. isExpired: boolean,
  38. disableLinkSharing: boolean,
  39. isSearchServiceConfigured: boolean,
  40. isSearchServiceReachable: boolean,
  41. isSearchScopeChildrenAsDefault: boolean,
  42. isEnabledMarp: boolean,
  43. drawioUri: string | null,
  44. rendererConfig: RendererConfig,
  45. skipSSR: boolean,
  46. ssrMaxRevisionBodyLength: number,
  47. };
  48. type IShareLinkRelatedPage = IPagePopulatedToShowRevision & PageDocument;
  49. superjson.registerCustom<IShareLinkRelatedPage, string>(
  50. {
  51. isApplicable: (v): v is IShareLinkRelatedPage => {
  52. return v != null
  53. && v.toObject != null
  54. && v.lastUpdateUser != null
  55. && v.creator != null
  56. && v.revision != null;
  57. },
  58. serialize: (v) => { return superjson.stringify(v.toObject()) },
  59. deserialize: (v) => { return superjson.parse(v) },
  60. },
  61. 'IShareLinkRelatedPageTransformer',
  62. );
  63. // GrowiContextualSubNavigation for shared page
  64. // get page info from props not to send request 'GET /page' from client
  65. type GrowiContextualSubNavigationForSharedPageProps = {
  66. page?: IPagePopulatedToShowRevision,
  67. isLinkSharingDisabled: boolean,
  68. }
  69. const GrowiContextualSubNavigationForSharedPage = (props: GrowiContextualSubNavigationForSharedPageProps): JSX.Element => {
  70. const { page, isLinkSharingDisabled } = props;
  71. return (
  72. <GrowiContextualSubNavigationSubstance currentPage={page} isLinkSharingDisabled={isLinkSharingDisabled} />
  73. );
  74. };
  75. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  76. useCurrentPathname(props.shareLink?.relatedPage.path);
  77. useIsSearchPage(false);
  78. useIsNotFound(props.isNotFound);
  79. useShareLinkId(props.shareLink?._id);
  80. useCurrentPageId(props.shareLink?.relatedPage._id);
  81. useCurrentUser(props.currentUser);
  82. useRendererConfig(props.rendererConfig);
  83. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  84. useIsSearchServiceReachable(props.isSearchServiceReachable);
  85. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  86. useIsEnabledMarp(props.rendererConfig.isEnabledMarp);
  87. useIsContainerFluid(props.isContainerFluid);
  88. const { trigger: mutateCurrentPage, data: currentPage } = useSWRMUTxCurrentPage();
  89. useEffect(() => {
  90. if (!props.skipSSR) {
  91. return;
  92. }
  93. if (props.shareLink?.relatedPage._id != null && !props.isNotFound) {
  94. mutateCurrentPage();
  95. }
  96. }, [mutateCurrentPage, props.isNotFound, props.shareLink?.relatedPage._id, props.skipSSR]);
  97. const pagePath = props.shareLinkRelatedPage?.path ?? '';
  98. const title = generateCustomTitleForPage(props, pagePath);
  99. return (
  100. <>
  101. <Head>
  102. <title>{title}</title>
  103. </Head>
  104. <div className="dynamic-layout-root justify-content-between">
  105. <GrowiContextualSubNavigationForSharedPage page={currentPage ?? props.shareLinkRelatedPage} isLinkSharingDisabled={props.disableLinkSharing} />
  106. <ShareLinkPageView
  107. pagePath={pagePath}
  108. rendererConfig={props.rendererConfig}
  109. page={currentPage ?? props.shareLinkRelatedPage}
  110. shareLink={props.shareLink}
  111. isExpired={props.isExpired}
  112. disableLinkSharing={props.disableLinkSharing}
  113. />
  114. </div>
  115. </>
  116. );
  117. };
  118. SharedPage.getLayout = function getLayout(page) {
  119. return (
  120. <>
  121. <DrawioViewerScript drawioUri={page.props.rendererConfig.drawioUri} />
  122. <ShareLinkLayout>{page}</ShareLinkLayout>
  123. </>
  124. );
  125. };
  126. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  127. const req: CrowiRequest = context.req as CrowiRequest;
  128. const { crowi } = req;
  129. const { configManager, searchService } = crowi;
  130. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  131. props.isSearchServiceConfigured = searchService.isConfigured;
  132. props.isSearchServiceReachable = searchService.isReachable;
  133. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  134. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  135. props.rendererConfig = {
  136. isSharedPage: true,
  137. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  138. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  139. isEnabledMarp: configManager.getConfig('crowi', 'customize:isEnabledMarp'),
  140. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  141. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  142. drawioUri: configManager.getConfig('crowi', 'app:drawioUri'),
  143. plantumlUri: configManager.getConfig('crowi', 'app:plantumlUri'),
  144. // XSS Options
  145. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
  146. sanitizeType: configManager.getConfig('markdown', 'markdown:rehypeSanitize:option'),
  147. customAttrWhitelist: JSON.parse(crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:attributes')),
  148. customTagWhitelist: crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:tagNames'),
  149. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  150. };
  151. props.ssrMaxRevisionBodyLength = configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
  152. }
  153. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  154. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  155. props._nextI18Next = nextI18NextConfig._nextI18Next;
  156. }
  157. function getAction(props: Props): SupportedActionType {
  158. let action: SupportedActionType;
  159. if (props.isExpired) {
  160. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  161. }
  162. else if (props.shareLink == null) {
  163. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  164. }
  165. else {
  166. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  167. }
  168. return action;
  169. }
  170. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  171. const req = context.req as CrowiRequest;
  172. const { crowi, params } = req;
  173. const result = await getServerSideCommonProps(context);
  174. if (!('props' in result)) {
  175. throw new Error('invalid getSSP result');
  176. }
  177. const props: Props = result.props as Props;
  178. try {
  179. const shareLink = await ShareLink.findOne({ _id: params.linkId }).populate('relatedPage');
  180. if (shareLink == null) {
  181. props.isNotFound = true;
  182. }
  183. else {
  184. props.isNotFound = false;
  185. props.isExpired = shareLink.isExpired();
  186. props.shareLink = shareLink.toObject();
  187. // retrieve Page
  188. const Page = crowi.model('Page');
  189. const relatedPage = await Page.findOne({ _id: getIdForRef(shareLink.relatedPage) });
  190. // determine whether skip SSR
  191. const ssrMaxRevisionBodyLength = crowi.configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
  192. props.skipSSR = await skipSSR(relatedPage, ssrMaxRevisionBodyLength);
  193. // populate
  194. props.shareLinkRelatedPage = await relatedPage.populateDataToShowRevision(props.skipSSR); // shouldExcludeBody = skipSSR
  195. }
  196. }
  197. catch (err) {
  198. logger.error(err);
  199. }
  200. injectServerConfigurations(context, props);
  201. await injectNextI18NextConfigurations(context, props);
  202. await addActivity(context, getAction(props));
  203. return {
  204. props,
  205. };
  206. };
  207. export default SharedPage;